//
//  TencentClient.m
//  AutoOwnersHome
//
//  Created by Raymond~ on 15/1/21.
//  Copyright (c) 2015年 mac. All rights reserved.
//

#import "TencentClient.h"
#import <TencentOpenAPI/sdkdef.h>
#import <TencentOpenAPI/TencentOAuth.h>
#import <TencentOpenAPI/QQApiInterface.h>
#import <TencentOpenAPI/QQApiInterfaceObject.h>
#import "NSString+KZExtension.h"

@interface TencentClient ()
{
    BOOL Status;
}
@property (nonatomic, strong) TencentOAuth * tencentOAuth;

@end
@implementation TencentClient
- (id)initTencentClient
{
    if (self=[super init]) {
        self.tencentOAuth = [[TencentOAuth alloc] initWithAppId:kQQAppID andDelegate:self];
    }
    return self;
}
+(TencentClient *)shareTencentClient{
    static TencentClient *singleClient;
    static dispatch_once_t pred;
    dispatch_once(&pred, ^{
        singleClient = [[TencentClient alloc] initTencentClient];
    });
    return singleClient;
}

- (void)login
{
    NSArray* permissions = [NSArray arrayWithObjects:@"get_user_info", @"get_simple_userinfo", @"add_t", nil];
    
    [self.tencentOAuth authorize:permissions inSafari:NO];
}

- (void)tencentDidLogin{
    Status = [self.tencentOAuth getUserInfo];
//[self.tencentOAuth accessToken]
}
- (void)getUserInfoResponse:(APIResponse*) response{
    if (Status) {
        self.userInfo = response.jsonResponse;
        [self.delegate tencentClient:self tencentToken:[self.tencentOAuth accessToken] tencentOpenId:[self.tencentOAuth openId]];
    }else{
        
    }
}
- (void)tencentDidNotLogin:(BOOL)cancelled{


}
- (void)tencentDidNotNetWork{

}
- (void)sendToQZone:(NSString *)url title:(NSString *)title desctiption:(NSString *)desctiption imageUrl:(NSString *)imageUrl{
    if (title.length > 128) {
        title = [title substringToIndex:127];
    }
    QQApiNewsObject * newsObj = [QQApiNewsObject objectWithURL:[NSURL URLWithString:url] title:title description:desctiption previewImageURL:[NSURL URLWithString:imageUrl]];
    SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:newsObj];
    //将内容分享到qq
    [QQApiInterface SendReqToQZone:req];

//        QQApiSendResultCode sent = [QQApiInterface sendReq:req];

}
- (void)sendQQ:(NSString *)shareString{
    QQApiTextObject *txtObj = [QQApiTextObject objectWithText:shareString];
    SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:txtObj];
    //将内容分享到qq
    [QQApiInterface sendReq:req];
}

- (void)sendToQQ:(NSString *)url title:(NSString *)title desctiption:(NSString *)desctiption imageUrl:(NSString *)imageUrl {
    QQApiNewsObject *newsObj = [QQApiNewsObject
                                objectWithURL:URL(url)
                                title:StringIsNullRetBlank(title)
                                description:StringIsNullRetBlank(desctiption)
                                previewImageURL:URL(imageUrl)];
    SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:newsObj];
    [QQApiInterface sendReq:req];
}

- (void)sendQQ:(UIImage *)image text:(NSString *)text{
    
    
    image = [self normalCompressImage:image toMaxFileSize:1000];//1M
    
    
    
    QQApiImageObject *imgObj = [QQApiImageObject objectWithData:UIImageJPEGRepresentation(image, 1)
                                               previewImageData:UIImageJPEGRepresentation(image, 1)
                                                          title:nil
                                                    description:nil];
    SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:imgObj];
    //将内容分享到qq
    [QQApiInterface sendReq:req];

}
- (void)responseDidReceived:(APIResponse*)response forMessage:(NSString *)message{

}
/**
 处理来至QQ的请求
 */
- (void)onReq:(QQBaseReq *)req{

}

/**
 处理来至QQ的响应
 */
- (void)onResp:(QQBaseResp *)resp{
    
    NSLog(@"%@",resp.extendInfo);
    NSLog(@"%@",resp.errorDescription);
    NSLog(@"%@",resp.result);
    NSLog(@"%d",resp.type);

    if (resp.type == ESENDMESSAGETOQQRESPTYPE) {
        if (StringIsNullOrEmpty(resp.errorDescription)) {
            BlockCallWithOneArg(self.doneBackBlock, nil);
        }
    }
    self.doneBackBlock = nil;
    
    
}

/**
 处理QQ在线状态的回调
 */
- (void)isOnlineResponse:(NSDictionary *)response{


}

-(UIImage *)normalCompressImage:(UIImage *)image toMaxFileSize:(NSInteger)maxFileSize{
    CGFloat compression = 0.9f;
    CGFloat maxCompression = 0.01f;
    NSData *imageData = UIImageJPEGRepresentation(image, compression);
    
    while ([imageData length] > maxFileSize && compression > maxCompression) {
        compression -= 0.02;
        imageData = UIImageJPEGRepresentation(image, compression);
    }
    UIImage *compressedImage = [UIImage imageWithData:imageData];
    return compressedImage;
}
@end
